﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.PRSC_IN100500UV;
using YLW.Plat.Jhfw.Model.JhfwHypb;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_PRSC_IN100500UV
    {
        public static string SourceAndScheduleInfoQuery_Response(GetJhfwHypbListOut jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //创建根对象
            var xmlRoot = new PRSC_IN100500UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = Guid.NewGuid().ToString() },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "PRSC_IN100500UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = receiver_name
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.3",
                            extension = sender_name
                        }
                    }
                },
                acknowledgement = new acknowledgement
                {
                    typeCode = jsonData.subcode == "success" ? "AA" : "AE",
                    targetMessage = new targetMessage
                    {
                        id = new id
                        {
                            root = "2.16.156.10011.2.5.1.1",
                            extension = id_extension
                        }
                    },
                    acknowledgementDetail = new acknowledgementDetail
                    {
                        text = new text
                        {
                            value = jsonData.submsg
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "CACT",
                    moodCode = "EVN",

                    queryAck = new queryAck
                    {
                        queryResponseCode = new queryResponseCode { code = jsonData.subcode == "success" ? "OK" : "error" },
                    }
                },
            };

            List<subject> subList = new List<subject>();

            if (jsonData != null && jsonData.data != null)
            {
                foreach (var listItem in jsonData.data)
                {
                    subList.Add(new subject
                    {
                        typeCode = "SUBJ",
                        schedule = new schedule
                        {
                            classCode = "SCH",
                            moodCode = "EVN",
                            resourceSlot = new resourceSlot
                            {
                                classCode = "SLOT",
                                moodCode = "EVN",
                                id = new id
                                {
                                    item = new item
                                    {
                                        root = "2.16.156.10011.2.5.1.20",
                                        extension = listItem.pbid
                                    }
                                },
                                profession = new profession
                                {
                                    code = listItem.zydm,
                                    codeSystem = "2.16.156.10011.2.3.2.62",
                                    codeSystemName = "医疗卫生机构业务科室分类与代码表",
                                    displayName = new displayName { value = listItem.zyms }
                                },
                                totalNumber = new totalNumber { value = listItem.zhs },
                                deptId = new deptId
                                {
                                    item = new item
                                    {
                                        root = "2.16.156.10011.1.4",
                                        extension = listItem.deptid
                                    }
                                },
                                code = new code
                                {
                                    code1 = listItem.classid,
                                    codeSystem = "2.16.156.10011.2.5.1.21",
                                    codeSystemName = "资源级别代码",
                                    Item = new displayName
                                    {
                                        value = listItem.classname,
                                    }
                                },
                                statusCode = new statusCode(),
                                effectiveTime = new effectiveTime(),
                                priorityCode = new priorityCode(),
                                directTarget = new directTarget
                                {
                                    typeCode = "DIR",
                                    IdentifiedEntity = new IdentifiedEntity
                                    {
                                        classCode = "CONS",
                                        id = new id
                                        {
                                            item = new item { root = "2.16.156.10011.1.4", extension = listItem.empid }
                                        },
                                        code = new code
                                        {
                                            code1 = listItem.title,
                                            codeSystem = "2.16.156.10011.2.3.1.209",
                                            codeSystemName = "专业技术职务类别代码表",
                                            Item = new displayName
                                            {
                                                value = "",
                                            }
                                        },
                                        Person = new Person
                                        {
                                            classCode = "PSN",
                                            determinerCode = "INSTANCE",
                                            id = new id
                                            {
                                                item = new item { root = "2.16.156.10011.1.3", extension = listItem.sfzh }
                                            },
                                            name = new name
                                            {
                                                item = new item
                                                {
                                                    part = new part
                                                    {
                                                        value = listItem.empname
                                                    }
                                                }
                                            },
                                        }
                                    }
                                },
                                timeFrame = new timeFrame
                                {
                                    code = new code
                                    {
                                        code1 = listItem.fsdzylx,
                                        codeSystem = "2.16.156.10011.2.5.1.22",
                                        codeSystemName = "排班资源类型代码",
                                        Item= new originalText
                                        {
                                            value = listItem.fsdzymc,
                                        }
                                    },
                                    totalFrameNumber = new totalFrameNumber { value = listItem.fsdzyzs },
                                    effectiveTime = new effectiveTime
                                    {
                                        low = new low { value = listItem.kssj },
                                        high = new high { value = listItem.jssj }
                                    },
                                    remainNumber = new remainNumber { value = listItem.syhs },
                                },
                                extension = new extension
                                {
                                    sjly = new value { value1 = listItem.sjly },
                                    lyxt = new value { value1 = listItem.lyxt },
                                    hoscode = new value { value1 = listItem.hoscode },
                                    pbid = new value { value1 = listItem.pbid },
                                    fsdzylx = new value { value1 = listItem.fsdzylx },
                                    deptid = new value { value1 = listItem.deptid },
                                    deptname = new value { value1 = listItem.deptname },
                                    classid = new value { value1 = listItem.classid },
                                    classname = new value { value1 = listItem.classname },
                                    empid = new value { value1 = listItem.empid },
                                    empname = new value { value1 = listItem.empname },
                                    sfzh = new value { value1 = listItem.sfzh },
                                    title = new value { value1 = listItem.title },
                                    zydm = new value { value1 = listItem.zydm },
                                    zyms = new value { value1 = listItem.zyms },
                                    fsdzymc = new value { value1 = listItem.fsdzymc },
                                    fsdzyzs = new value { value1 = listItem.fsdzyzs },
                                    kssj = new value { value1 = listItem.kssj },
                                    jssj = new value { value1 = listItem.jssj },
                                    zhs = new value { value1 = listItem.zhs },
                                    syhs = new value { value1 = listItem.syhs },
                                    pbzt = new value { value1 = listItem.pbzt },
                                    pb_empid = new value { value1 = listItem.pb_empid },
                                    pb_empname = new value { value1 = listItem.pb_empname },
                                    useflag = new value { value1 = listItem.useflag },
                                    in_empid = new value { value1 = listItem.in_empid },
                                    in_date = new value { value1 = listItem.in_date },
                                    xg_empid = new value { value1 = listItem.xg_empid },
                                    xgsj = new value { value1 = listItem.xgsj }
                                }
                            }
                        }
                    });
                }
            }

            xmlRoot.controlActProcess.subject = subList.ToArray();

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
